package com.sxm.yiwei.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxm.yiwei.R;
import com.sxm.yiwei.beans.ShopList;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by KuKa on 2017/6/12.
 * 首页店铺中服务列表gridview （最多6个）
 */

public class HomeGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<ShopList.DataBean.ServiceListBean> homegridlist ;
   public HomeGridItemOnClickListener homeGridItemOnClickListener;
    public HomeGridViewAdapter(Context context, List<ShopList.DataBean.ServiceListBean> homegridlist ) {
        this.context=context;
        this.homegridlist=homegridlist;
    }

    @Override
    public int getCount() {
        return homegridlist.size();
    }

    @Override
    public Object getItem(int position) {
        return homegridlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder = null;
        if (convertView==null){
            view= LayoutInflater.from(context).inflate(R.layout.gridview_item,null);
            holder = new ViewHolder(view,position);
           /* holder.gridimg = (ImageView) view.findViewById(R.id.gridimg);
            holder.gridtype = (TextView) view.findViewById(R.id.grid_type);
            holder.gridtypecontent = (TextView) view.findViewById(R.id.grid_typecontent);
            holder.gridnumber = (TextView) view.findViewById(R.id.grid_number);
            holder.gridnumbercontent = (TextView) view.findViewById(R.id.grid_numbercontent);
            holder.gridtitle = (LinearLayout) view.findViewById(R.id.home_gridtitle);
            view.setTag(holder);*/
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
       /* int unit = homegridlist.get(position).getUnit();
       switch (unit){
           case 0:
               holder.gridtypecontent.setText(homegridlist.get(position).getAmount()+" 元/单");
               break;
           case 1:
               holder.gridtypecontent.setText(homegridlist.get(position).getAmount()+" 元/时");
               break;
           case 2:
               holder.gridtypecontent.setText(homegridlist.get(position).getAmount()+" 元/天");
               break;
           case 3:
               holder.gridtypecontent.setText(homegridlist.get(position).getAmount()+" 元/月");
               break;
           case 4:
               holder.gridtypecontent.setText(homegridlist.get(position).getAmount()+" 元/单");
               break;
       }
        holder.gridtype.setText(homegridlist.get(position).getName());
        holder.gridnumbercontent.setText(homegridlist.get(position).getVolume()+" 单");
        holder.gridimg.setImageBitmap(null);
        holder.gridimg.setTag(homegridlist.get(position).getServiceAvatar());
        ImageOptions.Builder options = new ImageOptions.Builder();
        options.setUseMemCache(true);

        if (holder.gridimg.getTag().equals(homegridlist.get(position).getServiceAvatar())){
            x.image().bind(holder.gridimg, InternetAddressUtis.IMG_URL+homegridlist.get(position).getServiceAvatar(),options.build());

        }else {}
        holder.gridtitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (homeGridItemOnClickListener!=null){
                    homeGridItemOnClickListener.homeGridItemOnClickListener(position);
                }
            }
        });*/
        return view;
    }

    public class  ViewHolder{
        private ImageView gridimg;
        private TextView gridtype,gridtypecontent,gridnumber,gridnumbercontent;
        public LinearLayout gridtitle;

        public ViewHolder(View view, final int p) {
            gridimg = (ImageView) view.findViewById(R.id.gridimg);
            gridtype = (TextView) view.findViewById(R.id.grid_type);
            gridtypecontent = (TextView) view.findViewById(R.id.grid_typecontent);
            gridnumber = (TextView) view.findViewById(R.id.grid_number);
            gridnumbercontent = (TextView) view.findViewById(R.id.grid_numbercontent);
            gridtitle = (LinearLayout) view.findViewById(R.id.home_gridtitle);
            int unit = homegridlist.get(p).getUnit();
            switch (unit){
                case 0:
                    gridtypecontent.setText(homegridlist.get(p).getAmount()+" 元/单");
                    break;
                case 1:
                    gridtypecontent.setText(homegridlist.get(p).getAmount()+" 元/时");
                    break;
                case 2:
                    gridtypecontent.setText(homegridlist.get(p).getAmount()+" 元/天");
                    break;
                case 3:
                    gridtypecontent.setText(homegridlist.get(p).getAmount()+" 元/月");
                    break;
                case 4:
                    gridtypecontent.setText(homegridlist.get(p).getAmount()+" 元/单");
                    break;
            }
            gridtype.setText(homegridlist.get(p).getName());
            gridnumbercontent.setText(homegridlist.get(p).getVolume()+" 单");
            gridimg.setImageBitmap(null);
            gridimg.setTag(homegridlist.get(p).getServiceAvatar());
            ImageOptions.Builder options = new ImageOptions.Builder();
            options.setUseMemCache(true);
            options.setLoadingDrawableId(R.mipmap.morentouxiang);
            options.setFailureDrawableId(R.mipmap.morentouxiang);
            if (gridimg.getTag().equals(homegridlist.get(p).getServiceAvatar())){
                x.image().bind(gridimg, InternetAddressUtis.IMG_URL+homegridlist.get(p).getServiceAvatar(),options.build());

            }else {}
            gridtitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (homeGridItemOnClickListener!=null){
                        homeGridItemOnClickListener.homeGridItemOnClickListener(p);
                    }
                }
            });

            view.setTag(this);
        }
    }
    public interface HomeGridItemOnClickListener{
        void homeGridItemOnClickListener(int i);
    }
    public void setHomeGridItemOnClickListener(HomeGridItemOnClickListener homeGridItemOnClickListener){
        this.homeGridItemOnClickListener=homeGridItemOnClickListener;
    }
}
